<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Newstype extends Controller
{
    private $_model;
    private $title_field = [

    ];
    public $pagesize = 10;

    public function _initialize()
    {
        $this->_model = new \app\admin\model\Newstype();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);
        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
    }

    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {

        return $this->fetch('index');
    }


    public function lists()
    {

        $ser_key = trim(input("request.ser_key"));//auth
        $ser_val = trim(input("request.ser_val"));//auth
        $ser_type = trim(input("request.ser_type"));//auth

        if ($ser_key && $ser_val) {
            $where[$ser_key] = array('like', '%' . $ser_val . '%');
        }


        $lists = $this->_model->lists($where, $this->pagesize);

        $this->assign('lists', $lists);

        $this->assign('request', input("request."));
        return view('lists');

    }


    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        //查询是否有文章  如果有文章 不能够删除
        $newsModel = new \app\admin\model\News();
        $newsInfo = $newsModel->getDataForWhere(array('ty' => $id));
        if (!empty($newsInfo)) {
            $return = ['err' => 1, 'msg' => '检查到该栏目下有文章，暂不能够删除'];
            return json($return);
        }
        $res = $this->_model->delData(array('id' => $id));

        if ($res) {


            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }


    }


    public function recordSystemLog($arr)
    {
        $model = new \app\admin\model\Log();
        $model->add($arr);
    }


    public function addData()
    {

        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $name = trim(input("request.name"));//auth

            if (empty($name)) {

                $return = ['err' => 3, 'msg' => '栏目名称不能为空'];
                return json($return);
            }
            $oldGroup = $this->_model->getDataForWhere(array(
                'name' => $name,

            ));
            if (!empty($oldGroup)) {
                $return = ['err' => 3, 'msg' => '栏目已经存在'];
                return json($return);
            }

            $sorts = trim(input("request.sorts"));//auth
            if (empty($sorts)) {
                $sorts = 0;
            }

            $insertData = array(
                'name' => $name,
                'sorts' => $sorts,
                'ctime' => time(),

            );
            $res = $this->_model->add($insertData);

            if ($res) {

                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }


        //设置全局信息
        $title = "添加栏目";
        $this->assign('title', $title);

        return view('addData');

    }


    public function infoData()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));


        $this->assign('info', $info);

        //设置全局信息
        $title = "栏目详情";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('infoData');

    }


    public function editData()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));

        if ($sys_opt) {
            $name = trim(input("request.name"));//auth

            $sorts = trim(input("request.sorts"));//auth
            if (empty($sorts)) {
                $sorts = 0;
            }
            $w = array(
                'id' => $id
            );
            $update = array(
                'name' => $name,
                'sorts' => $sorts,
            );
            $res = $this->_model->updateDataForWhere($update, $w);

            if ($res || ($res === 0)) {

                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }

        $this->assign('info', $info);


        //设置全局信息
        $title = "编辑栏目";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('editData');

    }


}